package com.github.pig.common.web;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.UserUtils;
import com.netflix.discovery.util.StringUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.List;

/**
 * @author lengleng
 * @date 2017/10/28
 */
public class BaseController {
    @Autowired
    private HttpServletRequest request;
    protected org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());

    /**
     * 根据请求heard中的token获取用户角色
     *
     * @return 角色名
     */
    public List<String> getRole() {
        return UserUtils.getRole(request);
    }

    public  Claims getCurrentUserInfo(){
        String token = UserUtils.getToken(request);
        if(StringUtils.isNotEmpty(token)){
            String key = Base64.getEncoder().encodeToString(CommonConstant.SIGN_KEY.getBytes());
            Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
            return claims;
        }else{
            return null;
        }

    }
    /**
     * 根据请求heard中的token获取用户
     *
     * @return 角色名
     */
    public  String getUserName(){
        Claims claims =getCurrentUserInfo();
        if(claims!=null){
            return (String) claims.get("user_name");
        }else{
            return null;
        }

    }

    public Page page(){
        return null;
    }

}
